---@class TimeBoxItem : UIComponentInterface
TimeBoxItem = Class("TimeBoxItem", UIComponentInterface)
local t = TimeBoxItem
t.lockType = {
    { Config.FuncOpenType.TimeBox1,
      Config.FuncOpenType.TimeBox2,
      Config.FuncOpenType.TimeBox3 },
    { Config.FuncOpenType.TimeBox4,
      Config.FuncOpenType.TimeBox5,
      Config.FuncOpenType.TimeBox6, },
}
t.REWARD_STATE = {
    cd = 0, --正在倒计时
    reward = 1, --可领取
    empty = 2, --空
    lock = 3,
    wait = 4, --放置宝箱等待倒计时中
}

---@param obj GComponent
function t:OnCreate(obj)
    self.obj = obj;
    self.bind = {
        icon = self:GetChild("icon"),
        frame = self:GetChild("frame"),
        quality = self:GetChild("quality"),
        time = self:GetChild("time"),
        keyIcon = self:GetChild("key"),
        keyNum = self:GetChild("keyNum"),
        unlockTip = self:GetChild("unlockTip"),
        ctrlState = self:GetController("ctrlState"),
        anim = self:GetChild("n26")
    }
    self:RegisterOnClick(obj, function(self,obj)
        self:ClickItem(obj)
    end )
    self.startTime = 0
    self.endTime = 0
    self.state = -1
end
function t:Refresh(sequence, index, stateChangeCallback, tb)
    logicMgr.FuncOpenManager.removeStatusCtr("TimeBoxItem");
    logicMgr.FuncOpenManager.addStatusCtr("TimeBoxItem",t.lockType[sequence][index], self.obj.name,self.obj);
    self.sequence = sequence
    self.index = index
    self.changeCallback = stateChangeCallback
    self.changeCallbackTB = tb
    self:RemoveTimeCall(self.UpdateTime)
    local lockType, param = TimeBoxHandler.GetBoxLockLevel(self.sequence, self.index)
    if lockType > 0 then
        self:Lock(lockType, param)
        return
    end
    self:SetQuality(self.bind.quality, 0)
    self:SetItemFrame(self.bind.frame, 0)
    local info = TimeBoxHandler.GetTimeBoxInfo()[sequence]
    if not info or not info.idList[index] then
        self:SetState(t.REWARD_STATE.empty)
    else
        local modelId = info.idList[index]
        self:SetItemAllById(self.bind.icon, self.bind.quality, self.bind.frame, modelId)
        --self:SetIconById(self.bind.icon,modelId,EnumConst.ItemShowType.ITEM)
        local cfg = dataTable.getRow("t_item", modelId)
        if not cfg then
            return
        end
        local cd, keyId = self:ParseItemFunc(cfg.f_ItemFunction)
        self:SetIconById(self.bind.keyIcon, keyId, EnumConst.ItemShowType.ITEM)
        self.bind.keyNum.text = "1"
        if logicMgr.ItemManager.getItemCountbyModelId(keyId) >= 1 then
            self.bind.keyNum.color = Color.green
        else
            self.bind.keyNum.color = Color.red
        end
        self.startTime, self.endTime = TimeBoxHandler.GetBoxTime(self.sequence, self.index)
        if self.endTime > 0 then
            self:TimeCall(self.UpdateTime, 1000, -1)
        end
        self:UpdateTime()
    end
end

function t:UpdateTime()
    local server = HelperFunc.GetServerTime()
    if server < self.startTime and self.index > 1 then
        --未到起始时间不倒计时，第一个格子总是倒计时
        self:SetState(t.REWARD_STATE.wait)
    else
        self:SetState(t.REWARD_STATE.cd)
        local cd = self.endTime - server
        if cd <= 0 then
            self:Reward()
            return true
        else
            self.bind.time.text = HelperFunc.GetTimeStr(cd)
        end
    end
end

--00:00:00

function t:ParseItemFunc(str)
    local tb = decodeJsonStrToTabel(str)
    return tb[2], tb[3]
end

function t:Reward()
    self:SetState(t.REWARD_STATE.reward)
end
function t:Empty()
    self:SetState(t.REWARD_STATE.empty)
end
function t:Lock(type, param)
    self:SetState(t.REWARD_STATE.lock)
    self.bind.unlockTip.text = ""
end
function t:SetState(state)
    if self.state ~= state then
        self.state = state
        self.bind.ctrlState.selectedIndex = state
        if state == t.REWARD_STATE.reward then
            self.bind.anim.visible = true
            Util.PlayAction(self.bind.anim.wrapTarget,"idle2",true)
            local index = table.containsIndex(TimeBoxHandler.CONST_BOX_ID_LIST,TimeBoxHandler.GetBoxId(self.sequence,self.index))
            self.bind.anim.skinName = tostring(index)
        else
            self.bind.anim.visible = false
        end
        if self.changeCallback then
            self.changeCallback(self.changeCallbackTB, self)
        end
    end
end
function t:OnDestroy()
    self.bind = nil
    self.state = -1
    self.changeCallback = nil
    self.changeCallbackTB = nil
end

function t:ClickItem()
    if FuncOpenManager.isOpen(t.lockType[self.sequence][self.index], true) then
        if self.state == t.REWARD_STATE.cd or self.state == t.REWARD_STATE.wait then
            logicMgr.UIShowManager.ShowTimeBoxQuickOpenInTask(self.sequence, self.index)
            --logicMgr.UIShowManager.ShowTimeBoxQuickOpenInBag(logicMgr.ItemManager.getItemIdByModelId(200501))
        elseif self.state == t.REWARD_STATE.empty then
            logicMgr.UIShowManager.ShowTimeBoxBag(self.sequence)
        elseif self.state == t.REWARD_STATE.reward then
            TimeBoxHandler.SetRewardShowId(TimeBoxHandler.GetBoxId(self.sequence,self.index))
            TimeBoxHandler.ReqTimeBoxReward(0, self.sequence - 1, self.index - 1)  --领取
        elseif self.state == t.REWARD_STATE.lock then
            local type, param = TimeBoxHandler.GetBoxLockLevel(self.sequence, self.index)
            UISysTips.AddMsg(param)
        end
    end
end